package GUI;

import Ctr.AttendanceCtr;
import Ctr.LoginCtr;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginGUI {
    private JPanel pnlLogin;
    private JComboBox cmbType;
    private JTextField txtUserName;
    private JTextField txtPassword;
    private JButton btnViewAttendance;
    private JButton btnCheckIn;
    private JPanel pnlButtons;
    private JLabel lblUsername;
    private JLabel lblPassword;
    private JLabel lblUserType;
    private JCheckBox chkOverskrivDato;
    private LoginCtr loginCtr;
    private AttendanceCtr attendanceCtr;

    public LoginGUI() {

        attendanceCtr = new AttendanceCtr();
        loginCtr = new LoginCtr();

        initializeComboBox();

        //ComboBox change
        cmbType.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setButtonValues();
            }
        });

        //Student check-in
        btnCheckIn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                studentCheckIn();
                clearAllFields();
            }
        });

        //View attendance / Teacher login
        btnViewAttendance.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginType();
            }
        });
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("LoginGUI");
        frame.setContentPane(new LoginGUI().pnlLogin);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setSize(new Dimension(400, 500));
        frame.setVisible(true);
    }

    //Initializing the combobox
    public void initializeComboBox () {
        String[] types = { "Student", "Teacher" };

        for (String s : types) {
            cmbType.addItem(s);
        }
        cmbType.setSelectedIndex(0);
    }

    //Method for clearing textfields
    public void clearAllFields() {
        txtUserName.setText("");
        txtPassword.setText("");
    }

    //Setting the values of the buttons, avoiding exceptions
    private void setButtonValues() {
        if (cmbType.getSelectedIndex() == 0) {
            btnViewAttendance.setText("Se fravær");
            btnCheckIn.setText("Check-in");
            btnCheckIn.setEnabled(true);
            clearAllFields();
        }
        else {
            btnViewAttendance.setText("Login");
            btnCheckIn.setEnabled(false);
            clearAllFields();
        }
    }

    //Warning window - prints out warnings
    private void warningWindow(String message) {
        JOptionPane.showMessageDialog(pnlLogin, "" + message);
    }

    //Student login
    private void studentLogin() throws NullPointerException {
        try {
            AttendanceGUI attendanceGUI = new AttendanceGUI(loginCtr.studentLogin(txtUserName.getText(), txtPassword.getText()), getCurrentDate());
            attendanceGUI.setVisible(true);
        }
        catch (NullPointerException npe) {
            warningWindow("Forkert brugernavn/password");
        }
    }

    //Teacher login
    private void teacherLogin() throws NullPointerException {
        try {
            TeacherGUI teacherGUI = new TeacherGUI(loginCtr.teacherLogin(txtUserName.getText(), txtPassword.getText()), getCurrentDate());
            teacherGUI.setVisible(true);
        }
        catch (NullPointerException npe) {
            warningWindow("Forkert brugernavn/password");
        }
    }

    //Student check-in
    private void studentCheckIn() {
        try {
            if (attendanceCtr.studentCheckIn(loginCtr.studentLogin(txtUserName.getText(), txtPassword.getText()), getCurrentDate()) == true) {
                warningWindow("Du er nu checket ind");
            }
        }
        catch (NullPointerException npe) {
            warningWindow("Forkert brugernavn/password og/eller ingen lektioner i dag");
        }
    }

    //Determines the type of user
    private void loginType() {
        if (cmbType.getSelectedIndex() == 0) {
            studentLogin();
        }
        else {
            teacherLogin();
        }
    }

    //Overskriv dato - demonstration
    private String getCurrentDate() {
        if (chkOverskrivDato.isSelected()) {
            return "2013-10-30";
        }
        else {
            return attendanceCtr.getCurrentDate();
        }
    }
}


